package me.jessyan.mvparms.demo.mvp.model.logic;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.jess.arms.utils.ArmsUtils;

import com.tbruyelle.rxpermissions2.RxPermissions;

/**
 * The author: Zhao X.H.
 * Creation time: 2018/2/6 ${time}
 */

public class PermissionUtil {
    public static boolean openEXTERNALPermission(Activity mActivity,int what){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(mActivity,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                // Should we show an explanation?
                if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    // 显示给用户的解释
                } else {
                    // No explanation needed, we can request the permission.
                    ActivityCompat.requestPermissions(mActivity,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            what);
                }
                return true;
            }
            return false;
        }
        return false;
    }

    public static void openCameraPermissionToScanActivity(Activity mActivity){
        if (ContextCompat.checkSelfPermission(mActivity,
                    Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
                // Should we show an explanation?
                RxPermissions mRxPermissions = new RxPermissions((FragmentActivity) mActivity);
                mRxPermissions
                        .request(Manifest.permission.CAMERA)
                        .subscribe(granted -> {
                            if (granted) { // Always true pre-M
                                //如果已经授权就直接跳转到二维码扫面界面

                            } else { // Oups permission denied
                                Toast.makeText(mActivity, "相机权限被拒绝，无法扫描二维码", Toast.LENGTH_SHORT).show();
                                return;
                            }
                        });
        }else {


        }
    }
}
